#!/usr/bin/env bash
set -euo pipefail

OHOS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENTRY_DIR="${OHOS_DIR}/entry"
DEP_DIR="${ENTRY_DIR}/oh_modules/@kuiklybase/knoi"

if [[ ! -d "${ENTRY_DIR}" ]]; then
  echo "entry module not found at ${ENTRY_DIR}" >&2
  exit 1
fi

echo "[refresh] entry dir: ${ENTRY_DIR}"
if [[ -d "${DEP_DIR}" ]]; then
  echo "[refresh] removing cached dependency: ${DEP_DIR}"
  rm -rf "${DEP_DIR}"
fi

pushd "${ENTRY_DIR}" >/dev/null
echo "[refresh] running ohpm install  (may take a while)..."
ohpm install
echo "[refresh] done ohpm install"
popd >/dev/null

cat <<'EOF'
[refresh] Finished syncing @kuiklybase/knoi.
Next steps:
  1. Rebuild the entry module in your HarmonyOS build flow (e.g., DevEco Studio).
  2. Reinstall the regenerated hap onto the target device.
EOF
